package top.zhongyy.controller.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.velocity.util.introspection.Uberspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import top.zhongyy.pojo.Tag;
import top.zhongyy.pojo.Tag;
import top.zhongyy.pojo.Type;
import top.zhongyy.service.TagService;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Controller
@RequestMapping("/admin")
@ApiModel("博客标签的后台控制器")
public class TagController {
    
    @Autowired
    private TagService tagService;
    
    
    /**
     * @param model
     * @param pageNum
     * @author Andy
     * @date: 2021/3/25 23:00
     */
    @ApiOperation(value = "标签的分页展示")
    @GetMapping("/tags")
    public String list(Model model,
                       @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //1、开启分页
        PageHelper.startPage(pageNum, 5, "id desc");
        List<Tag> list = tagService.list();
        //2、封装list到 PageInfo对象中自动分页
        PageInfo<Tag> pageInfo = new PageInfo<>(list);
        model.addAttribute("page", pageInfo);
        return "admin/tags";
    }
    
    
    @ApiOperation(value = "跳转到添加标签页面")
    @GetMapping("/tags/input")
    public String toAdd(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags_release";
    }
    
    
    /**
     * @param tag
     * @param attributes
     * @author Andy
     * @date: 2021/3/25 23:03
     */
    @ApiOperation(value = "添加标签")
    @PostMapping("/tags")
    public String addTag(@Validated Tag tag, RedirectAttributes attributes) {
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/tags/input";
        }
        boolean save = tagService.save(tag);
        if (save) {
            attributes.addFlashAttribute("message", "新增成功");
        } else {
            attributes.addFlashAttribute("message", "新增失败");
        }
        return "redirect:/admin/tags";
    }
    
    @ApiOperation(value = "跳转到修改标签页面")
    @GetMapping("/tags/{id}/input")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        model.addAttribute("tag", tagService.getById(id));
        return "admin/tags_release";
    }
    
    /**
     * @param tag
     * @param result
     * @param id
     * @param attributes
     * @author Andy
     * @date: 2021/3/25 23:21
     */
    @ApiOperation(value = "修改标签提交")
    @PostMapping("/tags/{id}")
    public String editPost(@Validated Tag tag, BindingResult result, @PathVariable("id") Long id, RedirectAttributes attributes) {
        Tag tag1 = tagService.getTagByName(tag.getName());
        if (tag != null) {
            result.rejectValue("name", "nameError", "标签已存在");
        }
        if (result.hasErrors()) {
            return "admin/tags_release";
        }
        boolean flag = tagService.updateById(tag);
        if (flag) {
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/tags";
    }
    
    
    /**
     * @param id
     * @param attributes
     * @author Andy
     * @date: 2021/3/25 23:21
     */
    @ApiOperation(value = "根据 id 删除标签")
    @GetMapping("/tags/{id}/delete")
    public String deleteById(@PathVariable("id") Long id, RedirectAttributes attributes) {
        boolean flag = tagService.removeById(id);
        if (flag) {
            attributes.addFlashAttribute("message", "删除成功");
        } else {
            attributes.addFlashAttribute("message", "删除失败");
        }
        return "redirect:/admin/tags";
        
    }
    
    
}

